package edu.hebeu.controller;

import com.baomidou.mybatisplus.plugins.Page;
import edu.hebeu.entity.Department;
import edu.hebeu.entity.Employee;
import edu.hebeu.entity.Leave;
import edu.hebeu.mapper.DepartmentMapper;
import edu.hebeu.mapper.EmployeeMapper;
import edu.hebeu.service.EmployeeService;
import edu.hebeu.service.LeaveService;
import edu.hebeu.util.MTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("leave")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @RequestMapping("/toAdd.do")
    public  String toAdd(){
        return "admin/leave_add";
    }
    //申请请假
    @RequestMapping("/add.do")
    public  String add(Leave leave, String start, String end,
                       @RequestParam Integer employeeNumber){
       Employee employee= employeeMapper.selectByNumber(employeeNumber);
        Integer departmentNumber = employee.getDepartmentNumber();
        Department department = departmentMapper.selectByNumber(departmentNumber);
        leave.setDepartmentNumber(department.getDepartmentNumber());
        leave.setNotes(department.getNotes());
         leave.setManager(department.getManager());
        leave.setStartTime(MTimeUtil.stringParse(start));
        leave.setEndTime(MTimeUtil.stringParse(end));
         leaveService.insert(leave);
        return  "forward:/leave/toAdd.do";
    }
    @RequestMapping("/oneself.do")
    public  String leave(@RequestParam Integer pageNo, Model model){
       Page<Leave> page= leaveService.selectPag(pageNo);
        model.addAttribute("page",page);
        return "admin/oneself_leave";
    }


}
